// pages/cmdView/sync/syncView.js
const util = require("../../../utils/util");
const queryAPI = requirePlugin("hello-plugin")
const healthDataAPI = requirePlugin("hello-plugin")
const {

  // API的状态码
  succeed,
  failed,
  timeout,
  unavailable,
  dataError,
  crcError,
  noRecord,

  // 同步历史数据
  YCHealthDataCommandID,
  queryHealthDataStep,
  queryHealthDataSleep,
  queryHealthDataHeartRate,
  queryHealthDataBloodPressure,
  queryHealthDataCombinedData,
  queryHealthDataInvasiveMeasurement,
  queryHealthDataHistoryBody,

  replyHealthDataStep,
  replyHealthDataSleep,
  replyHealthDataHeartRate,
  replyHealthDataBloodPressure,
  replyHealthDataCombinedData,
  replyHealthDataInvasiveMeasurement,

  deleteHealthDataStep,
  deleteHealthDataSleep,
  deleteHealthDataHeartRate,
  deleteHealthDataBloodPressure,
  deleteHealthDataCombinedData,
  deleteHealthDataHistoryBody,
  deleteHealthDataInvasiveMeasurement,

} = require("../../../utils/constant")
const {
  handleToast,
  closeToast
} = require("../../../utils/alertUtil")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    canvasText: "获取蓝牙设备信息："
  },
  // mark: 同步运动数据
  syncStep() {
    var that = this
    handleToast("命令发送中……", "loading", 0)
    // 同步步数
    healthDataAPI.queryDeviceHealthData(queryHealthDataStep, function (state, response) {
      closeToast()
      util.printLog('收到步数数据')
      if (state != succeed &&
        state != noRecord) {
        return
      }
      that.setData({
        canvasText: that.data.canvasText + "\rsport:" + JSON.stringify(response)
      })
      // response.forEach(info => {
      //   util.printLog('==== 详细数据')
      //   util.printLog(info)
      //   util.printLog(info.startTime + ', ' + info.endTime + ',' + info.step + ',' + info.distance + ',' + info.calories)
      // });

      healthDataAPI.deleteDeviceHealthData(deleteHealthDataStep, function (state, response) {
        that.setData({
          canvasText: that.data.canvasText + "\r删除步数:" + JSON.stringify(response)
        })
        // util.printLog('删除步数', state)
      })
    })
  },
  // mark: 同步睡眠数据
  syncSleep() {
    var that = this
    handleToast("命令发送中……", "loading", 0)
    // 同步睡眠
    healthDataAPI.queryDeviceHealthData(queryHealthDataSleep, function (state, response) {
      util.printLog('收到睡眠数据')
      closeToast()
      if (state != succeed &&
        state != noRecord) {
        return
      }

      // response.forEach(info => {
      //   util.printLog(info.startTime + ', ' + info.endTime)
      // });

      that.setData({
        canvasText: that.data.canvasText + "\n同步的睡眠数据:" + JSON.stringify(response)
      })

      healthDataAPI.deleteDeviceHealthData(deleteHealthDataSleep, function (state, response) {
        // util.printLog('删除睡眠' + state)
        that.setData({
          canvasText: that.data.canvasText + "\r删除睡眠:" + JSON.stringify(response)
        })
      })
    })
  },
  // mark: 同步心率数据
  syncheaderLine() {
    var that = this
    handleToast("命令发送中……", "loading", 0)
    healthDataAPI.queryDeviceHealthData(queryHealthDataHeartRate, function (state, response) {
      closeToast()
      util.printLog('收到心率数据')
      if (state != succeed &&
        state != noRecord) {
        return
      }
      // response.forEach(info => {
      //   util.printLog(info.startTime + ', ' + info.heartRate)
      // });

      that.setData({
        canvasText: that.data.canvasText + "\n同步的心率数据:" + JSON.stringify(response)
      })

      healthDataAPI.deleteDeviceHealthData(deleteHealthDataHeartRate, function (state, response) {
        // util.printLog('删除心率' + state)
        that.setData({
          canvasText: that.data.canvasText + "\r删除心率:" + JSON.stringify(response)
        })

      })

    })
  },
  // mark: 同步血压数据
  syncBloodPressure() {
    var that = this
    handleToast("命令发送中……", "loading", 0)
    // 同步血压
    healthDataAPI.queryDeviceHealthData(queryHealthDataBloodPressure, function (state, response) {
      if (state != succeed &&
        state != noRecord) {
        return
      }
      that.setData({
        canvasText: that.data.canvasText + "\n同步的血压数据:" + JSON.stringify(response)
      })
      healthDataAPI.deleteDeviceHealthData(deleteHealthDataBloodPressure, function (state, response) {
        // util.printLog('删除血压' + state)
        that.setData({
          canvasText: that.data.canvasText + "\r删除血压:" + JSON.stringify(response)
        })
      })
      closeToast()

    })
  },

  // mark: 同步hrv数据
  syncHrv() {
    var that = this
    handleToast("命令发送中……", "loading", 0)
    // 同步血压
    healthDataAPI.queryDeviceHealthData(queryHealthDataCombinedData, function (state, response) {
      if (state != succeed &&
        state != noRecord) {
        return
      }
      that.setData({
        canvasText: that.data.canvasText + "\n同步的hrv数据:" + JSON.stringify(response)
      })
      // healthDataAPI.deleteDeviceHealthData(deleteHealthDataBloodPressure, function (state, response) {
      //   // util.printLog('删除血压' + state)
      //   that.setData({
      //     canvasText: that.data.canvasText + "\r删除血压:" + JSON.stringify(state)
      //   })
      // })
      closeToast()

    })
  },

    // mark: 同步压力数据
    syncPressure() {
      var that = this
      handleToast("命令发送中……", "loading", 0)
      // 同步压力
      healthDataAPI.queryDeviceHealthData(queryHealthDataHistoryBody, function (state, response) {
        if (state != succeed &&
          state != noRecord) {
          return
        }
        that.setData({
          canvasText: that.data.canvasText + "\n同步的数据:" + JSON.stringify(response)
        })
        healthDataAPI.deleteDeviceHealthData(deleteHealthDataHistoryBody, function (state, response) {
          // util.printLog('删除压力' + state)
          that.setData({
            canvasText: that.data.canvasText + "\r删除压力:" + JSON.stringify(response)
          })
        })
        closeToast()
  
      })
    },


  // mark: 同步有创测量数据(血脂/尿酸)
  syncInvasive() {
    var that = this
    handleToast("命令发送中……", "loading", 0)
    // 同步血脂/尿酸
    healthDataAPI.queryDeviceHealthData(queryHealthDataInvasiveMeasurement, function (state, response) {
      console.log(state + " " + noRecord)
      if (state != succeed &&
        state != noRecord) {
        return
      }
      console.log(response)
      that.setData({
        canvasText: that.data.canvasText + "\n同步的血脂/尿酸数据:" + JSON.stringify(response)
      })
      healthDataAPI.deleteDeviceHealthData(queryHealthDataInvasiveMeasurement, function (state, response) {
        // util.printLog('删除血脂/尿酸' + state)
        that.setData({
          canvasText: that.data.canvasText + "\r删除血脂/尿酸:" + JSON.stringify(response)
        })
      })
      closeToast()

    })
  },
  // mark: 同步组合数据
  queryDeviceHealthData() {
    var that = this
    handleToast("命令发送中……", "loading", 0)
    healthDataAPI.queryDeviceHealthData(queryHealthDataCombinedData, function (state, response) {

      if (state != succeed &&
        state != noRecord) {
        return
      }
      that.setData({
        canvasText: that.data.canvasText + "\n同步组合数据:" + JSON.stringify(response)
      })

      healthDataAPI.deleteDeviceHealthData(deleteHealthDataCombinedData, function (state, response) {
        // util.printLog('删除组合数据' + state)
        that.setData({
          canvasText: that.data.canvasText + "\r删除组合数据:" + JSON.stringify(response)
        })
      })
      closeToast()

    })

  },














  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },


})